package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.dingtalk.openapi.vo.oa.InstanceAddDTO;
import com.alibaba.dingtalk.openapi.vo.oa.InstanceResultDTO;
import com.alibaba.dingtalk.openapi.vo.oa.InstanceUpdateDTO;
import com.alibaba.dingtalk.openapi.vo.oa.TaskAddDTO;
import com.alibaba.dingtalk.openapi.vo.oa.TaskCancelDTO;
import com.alibaba.dingtalk.openapi.vo.oa.TaskResultDTO;
import com.alibaba.dingtalk.openapi.vo.oa.TaskUpdateDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.constant.UrlConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 钉钉OA待办帮助类
 *
 * @author LiZhikang
 * @date 2023/3/14 15:45
 */
@Component
@Slf4j
public class DingtalkOaHelper {

    @Resource
    private RestTemplateHelper restTemplateHelper;

    /**
     * 创建钉钉OA实例
     *
     * @param instanceAddDTO
     * @param accessToken
     * @return
     */
    public InstanceResultDTO createDingtalkInstance(InstanceAddDTO instanceAddDTO, String accessToken) {
        String dingtalkUrl = formatUrl(UrlConstant.DINGTALK_OA_INSTANCE);
        JSONObject header = formatHeader(accessToken);
        JSONObject param = formatParam(instanceAddDTO);
        try {
            String response = restTemplateHelper.post(dingtalkUrl, param, header, null);
            String result = JSONObject.parseObject(response).getString(StringConstant.RESULT);
            return JSONObject.parseObject(result, InstanceResultDTO.class);
        } catch (Exception e) {
            log.error("DingtalkOaHelper.createDingtalkInstance fail", e);
            log.error(param.toJSONString());
        }
        return null;
    }

    /**
     * 更新钉钉OA实例
     *
     * @param instanceUpdateDTO
     * @param accessToken
     */
    public void updateDingtalkInstance(InstanceUpdateDTO instanceUpdateDTO, String accessToken) {
        String dingtalkUrl = formatUrl(UrlConstant.DINGTALK_OA_INSTANCE);
        JSONObject header = formatHeader(accessToken);
        JSONObject param = formatParam(instanceUpdateDTO);
        try {
            restTemplateHelper.put(dingtalkUrl, param, header, null);
        } catch (Exception e) {
            log.error("DingtalkOaHelper.updateDingtalkInstance fail", e);
            log.error(param.toJSONString());
        }
    }

    /**
     * 创建钉钉待办任务
     *
     * @param taskAddDTO
     * @param accessToken
     * @return
     */
    public List<TaskResultDTO> createDingtalkTask(TaskAddDTO taskAddDTO, String accessToken) {
        String dingtalkUrl = formatUrl(UrlConstant.DINGTALK_OA_TASK);
        JSONObject header = formatHeader(accessToken);
        JSONObject param = formatParam(taskAddDTO);
        try {
            String response = restTemplateHelper.post(dingtalkUrl, param, header, null);
            String result = JSONObject.parseObject(response).getString(StringConstant.RESULT);
            return JSONArray.parseArray(result, TaskResultDTO.class);
        } catch (Exception e) {
            log.error("DingtalkOaHelper.createDingtalkTask fail", e);
            log.error(param.toJSONString());
        }
        return null;
    }

    /**
     * 更新钉钉待办任务
     *
     * @param taskUpdateDTO
     * @param accessToken
     */
    public void updateDingtalkTask(TaskUpdateDTO taskUpdateDTO, String accessToken) {
        String dingtalkUrl = formatUrl(UrlConstant.DINGTALK_OA_TASK);
        JSONObject header = formatHeader(accessToken);
        JSONObject param = formatParam(taskUpdateDTO);
        try {
            restTemplateHelper.put(dingtalkUrl, param, header, null);
        } catch (Exception e) {
            log.error("DingtalkOaHelper.updateDingtalkTask fail", e);
            log.error(param.toJSONString());
        }
    }

    /**
     * 取消钉钉待办任务
     *
     * @param taskCancelDTO
     * @param accessToken
     */
    public void cancelDingtalkTask(TaskCancelDTO taskCancelDTO, String accessToken) {
        String dingtalkUrl = formatUrl(UrlConstant.DINGTALK_OA_TASK_CANCEL);
        JSONObject header = formatHeader(accessToken);
        JSONObject param = formatParam(taskCancelDTO);
        try {
            restTemplateHelper.post(dingtalkUrl, param, header, null);
        } catch (Exception e) {
            log.error("DingtalkOaHelper.updateDingtalkTask fail", e);
            log.error(param.toJSONString());
        }
    }

    /**
     * 格式化请求
     *
     * @param url
     * @return
     */
    private String formatUrl(String url) {
        return Env.API_HOST + url;
    }

    /**
     * 格式化请求头
     *
     * @param accessToken
     * @return
     */
    private JSONObject formatHeader(String accessToken) {
        JSONObject header = new JSONObject();
        header.put("x-acs-dingtalk-access-token", accessToken);
        return header;
    }

    /**
     * 格式化参数
     *
     * @param object
     * @return
     */
    private JSONObject formatParam(Object object) {
        return JSONObject.parseObject(JSON.toJSONString(object));
    }

}
